package cumeo89.chii.client.ui;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

import cumeo89.chii.client.images.ImageFactory;
import cumeo89.chii.client.util.MyChangeListener;
import cumeo89.chii.client.util.MyChangeListenerCollection;

public class HeaderPanel extends Composite {

	private TrivialPanel pnlContainer, pnlHeader;
	private Image imgCollapse, imgExpand;
	private Widget title, content;
	private HeaderPanelGroup group;
	private MyChangeListenerCollection listeners = new MyChangeListenerCollection();

	public HeaderPanel() {
		pnlContainer = new TrivialPanel("div");
		pnlContainer.setStyleName("hpanel");
		initWidget(pnlContainer);

		pnlHeader = new TrivialPanel("div");
		pnlHeader.setStyleName("hpanel-header");

		imgCollapse = ImageFactory.IMAGES.collapse().createImage();
		imgCollapse.setStyleName("hpanel-button");
		imgCollapse.setTitle("Thu nhỏ");
		imgCollapse.sinkEvents(Event.ONCLICK);
		pnlHeader.add(imgCollapse);

		imgExpand = ImageFactory.IMAGES.expand().createImage();
		imgExpand.setStyleName("hpanel-button");
		imgExpand.setTitle("Phóng to");
		imgExpand.sinkEvents(Event.ONCLICK);
		pnlHeader.add(imgExpand);

		pnlContainer.add(pnlHeader);

		internalSetExpanded(true);
	}

	@Override
	protected void onAttach() {
		super.onAttach();
		DOM.setEventListener(imgCollapse.getElement(), this);
		DOM.setEventListener(imgExpand.getElement(), this);
	}

	@Override
	public void onBrowserEvent(Event event) {
		if (event.getTypeInt() == Event.ONCLICK) {
			setExpanded(!isExpanded());
			event.stopPropagation();
			event.preventDefault();
		} else {
			super.onBrowserEvent(event);
		}
	}

	public Widget getTitleWidget() {
		return title;
	}

	public void setTitleWidget(Widget title) {
		if (this.title != null) {
			this.title.removeFromParent();
		}
		this.title = title;
		if (title != null) {
			pnlHeader.add(title);
		}
	}

	public Widget getContentWidget() {
		return content;
	}

	public void setContentWidget(Widget content) {
		if (this.content != null) {
			this.content.removeFromParent();
		}
		this.content = content;
		if (content != null) {
			pnlContainer.add(content);
		}
	}

	public boolean isExpanded() {
		return imgCollapse.isVisible();
	}

	public void setExpanded(boolean b) {
		if (b == isExpanded()) {
			return;
		}
		if (group != null) {
			group.setExpanded(this, b);
		} else {
			internalSetExpanded(b);
		}
	}

	void internalSetExpanded(boolean b) {
		if (b) {
			this.removeStyleDependentName("collapsed");
		} else {
			this.addStyleDependentName("collapsed");
		}
		imgExpand.setVisible(!b);
		imgCollapse.setVisible(b);
		if (content != null) {
			content.setVisible(b);
		}
		listeners.fireChange(this);
	}

	public HeaderPanelGroup getGroup() {
		return group;
	}

	void setGroup(HeaderPanelGroup group) {
		this.group = group;
	}

	public void addChangeListener(MyChangeListener lis) {
		if (!listeners.contains(lis)) {
			listeners.add(lis);
		}
	}

	public boolean removeMyChangeListener(MyChangeListener lis) {
		return listeners.remove(lis);
	}

}
